#include "../includes.h"


char beep_pps_flag;
char beep_alarm_flag;
int  beep_pps_counter, beep_alarm_counter;
char led_board_fail_flag;

unsigned char led_second;


PowerSensing_ ps={0, 0, 0, 0.1, 0, 0, 0, 0};




int iLEDgpndu=0;
int iLEDfd;

int led_blk_cnt=0;
int led_sync_retain_cnt=0;


//int led_blk[5]={ ON_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN }; 
//int led_blk[8]={ ON_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN };
int led_blk[8]={ OFF_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN, OFF_PATTERN };

void Init_LED(int fd)
{
	iLEDfd = fd;
	iLEDgpndu=0;
	SetLEDPort(iLEDfd);
	SetLedOeLow(iLEDfd);
}

int prevSecondForLedBlink=0;
void DisplayLEDgpndu()
{
	int i;
	int v, v_led;
	int bit;
	
	if( iLEDfd<=0) return ;
	
	//ps.cnt++;
	
	// Extract LED Toggle or On/Off
	if( led_blk_cnt > 9 || led_blk_cnt< 0){
	//if( led_blk_cnt > 15 || led_blk_cnt< 0){
		led_blk_cnt = 0;
	}
	
	//if(prevSecondForLedBlink!=SYSTEM.TIME.CUR.SECOND  && ( SYSTEM.TIME.CUR.SECOND % 4 == 0) && led_blk_cnt != 0){
	if((prevSecondForLedBlink!=led_second)  && (led_second % 4 == 0) && (led_blk_cnt != 0)){
		sprintf(MESG,"\n#### LedSync (%d,%d, %d)####\n",
			prevSecondForLedBlink,led_second,led_blk_cnt
		);
		MessageOut(MESG);
		SyncLEDDisplay();
	}
	
	//prevSecondForLedBlink=SYSTEM.TIME.CUR.SECOND;
	prevSecondForLedBlink=led_second;

	bit = 0x1 << led_blk_cnt;

	v_led = 0;

	for( i=0; i<8; i++){ //led number
		v_led <<=1;
		v_led |= ((led_blk[7-i]&bit) !=0 );
		
		//if( (led_blk[4-i]&bit) !=0) printf(" [0x%03X,1,0x%03X]",led_blk[4-i],(led_blk[4-i]&bit));
		//else printf(" [0x%03X,0,0x%03X]",led_blk[4-i],(led_blk[4-i]&bit));
	}

	//printf("vled:0x%02X: %05d 0x%02X \n",v_led, led_blk_cnt, bit);
	//printf("==>0x%03X %d\n ", v_led, led_blk_cnt);

	
	//kang v = ( iLEDgpndu & 0xE0 ) | v_led;
	v = v_led; 
	
	iLEDgpndu = v;

	led_blk_cnt++;

	// Start LED Display
	SetLedLatchLow(iLEDfd);	
	for( i=0; i<8; i++){
		SetLedClkLow(iLEDfd);
		if( v & 0x80 ) 	SetLedDinHigh(iLEDfd); //0x1<<7 is the same as 0x80
		else  			SetLedDinLow(iLEDfd);
		SetLedClkHigh(iLEDfd);
		v = v<<1;
	}	
	SetLedClkLow(iLEDfd);
	SetLedLatchHigh(iLEDfd);
	
}



unsigned ant_open_time = 0;
unsigned ant_short_time = 0;
unsigned no_satellite_time = 0;
unsigned ant_ok_logged = 0;
unsigned int holdover_time = 0;
char holdover_time_after24H = HOLDOVER24_NO;
char over_1us = NO;

int iLedTestCnt=0;
#define M_iLedTestCnt 2
int iQueryFANError=0;
void DisplayLED()
{


	led_board_fail_flag = OFF;

	// LED Test Routine
	if( iLsiDebugVariable == 520 ){ // for LED H/W test
		if( iLedTestCnt++ > M_iLedTestCnt*2 ) iLedTestCnt =0;
		if( iLedTestCnt > M_iLedTestCnt ){
			led_run_red_blk2();
			led_gps_red_blk2();
			led_clk_red_blk2();
			led_ant_red_blk2();
		}
		else{
			led_run_grn_blk2();
			led_gps_grn_blk2();
			led_clk_grn_blk2();
			led_ant_grn_blk2();
		}
		return;		
	}

	if( led_sync_retain_cnt > 0 ) led_sync_retain_cnt--;
	else led_sync_retain_cnt=0; // dummy code for abnormal case which led_sync_retain_cnt is <0

	////////////////////////////////////////////////////
	//RUN LED-----------------------------------------------
	led_run_grn_on();

	////////////////////////////////////////////////////
	//CLK LED-----------------------------------------------

	if( DIAG._10MHz == FAIL ){
		SetClk_na();
		led_clk_red_on();
		SetRCUClkFail();
	}
	else if( !QueryCLK_Heatup_Done() 		){
		//SetClk_na();
		led_clk_red_blk2();
		//SetRCUClkFail();
	}
	else if( QueryActive_HoldoverAfter24H() 	){ // in rcu-g not 24hour but 2weeks
		SetClk_na();
		//led_clk_grn_blk4();
		led_clk_red_blk4();
		ResetRCUClkFail();
	}
	else if( QueryActive_HoldoverWithin24H() ){ // in rcu-g not 24hour but 2weeks
		led_clk_grn_blk1();
		ResetRCUClkFail();
	}
	else{
		ClrClk_na();
		led_clk_grn_on();
		ResetRCUClkFail();
	}

//	if( DIAG.SYSTEM == OK ) ResetRCUFuncFail();
//	else SetRCUFuncFail();

	////////////////////////////////////////////////////
	// GPS LED
	if( QueryOCXOHeatUp() ){
		led_gps_grn_blk2();
	}
	else if( QueryAntOpenOrShort() ){ //QueryGPSAntOpen() || QueryGPSAntShort()){
		//led_gps_red_on();
		led_gps_grn_off();
		led_gps_red_off();
	}
	else if( holdover_time_after24H == HOLDOVER24_NOT_YET  )	{
		//SetGpsHoldover();
		//QueryActive_HoldoverWithin24H()
		if ( SYSTEM.OPERATION.STATUS == STATUS_ACTIVE ) led_gps_grn_blk3();
		else { led_gps_red_blk2(); }
	}
	else if( QueryActive_HoldoverAfter24H() ){
		//SetGpsHoldover();
		led_gps_red_blk3();
	}
	else
	{
		//ClrGpsHoldover();
		led_gps_grn_on();
	}
	
	////////////////////////////////////////////////////
	// ANT LED
	
	if(SYSTEM.EB.ANTENNA == OK){
		ClrAntOpen(); ClrAntShort();
		led_ant_grn_on();
		ant_open_time = 0; 
		ant_short_time = 0;
	}

	if(SYSTEM.EB.ANTENNA == ANTENNA_SHORTED || frc_error.ant == 2 ){
		ant_short_time++;
		if( IsAntennaFaultLimit(ant_short_time) || frc_error.ant == 2 ){
			SetAntShort();
			//printf("SetAntShort\n");
			led_ant_red_blk2();
			ant_open_time = 0;
		}
	}
	else{
		ClrAntShort();
	}
	
	if(SYSTEM.EB.ANTENNA == ANTENNA_OPENED || frc_error.ant == 1 ){
		ant_open_time++;
		if( IsAntennaFaultLimit(ant_open_time) || frc_error.ant == 1 ){
			SetAntOpen();
			led_ant_red_blk4();
			ant_short_time = 0;
		}
	}
	else{
		ClrAntOpen();
	}


	// Holdover time caculation---------- not LED operation
	//if(ant_open_time > 59+60*59){
	if(SYSTEM.OPERATION.LOCKMODE == HOLDOVER ||
		SYSTEM.OPERATION.LOCKMODE == HOLDOVER_OVER10MIN)
	{
		holdover_time++;
	}
	else{
		//hmlee - there are only three (HOLDOVER_OVER10MIN, HOLDOVER, LOCKED) conditions
		holdover_time = 0;
	}
	
	//if( holdover_time >= 86400*7 ){ // in rcu-g , 2 weeks is holdover limit
	if( holdover_time >= alm_range.holdover_warn2 || frc_error.holdover == 2 ){ // in rcu-g , 2 weeks is holdover limit
		SetClk_na();
		SetGpsHoldover();
		holdover_time_after24H = HOLDOVER24_YES;
	}
	//else if( holdover_time > 0 ){
	else if( holdover_time > alm_range.holdover_warn1  || frc_error.holdover == 1){
		SetGpsHoldover();
		holdover_time_after24H = HOLDOVER24_NOT_YET;
	}
	else{
		ClrGpsHoldover();
		holdover_time_after24H = HOLDOVER24_NO;
	}

	if( (SYSTEM.TRACK.VISIBLE == 0) || (SYSTEM.TRACK.TRACKED == 0) ){
		no_satellite_time++;
		if( no_satellite_time > 59 ){
			no_satellite_time = 100;
		}
	}
	else{
		no_satellite_time = 0;
	}

	TASK.C10msLED = 0;
	
	led_second = SYSTEM.TIME.TOUT.SECOND;
	
	return;
}


void LEDonResetOperation()
{
	led_run_grn_off(); led_run_red_on();
	led_act_grn_off(); led_act_red_on();
	led_clk_grn_off(); led_clk_red_on();
	
	//hmlee gps_red_off????
	//led_gps_grn_off(); led_gps_red_off();
	led_gps_grn_off(); led_gps_red_on();
}


int sync15_reset_min = 40;
//int zero_tracking = 0;
int reset_type_toggle = 0;


void eb_40min_off(void)
{
	int i;
	char sat_tracked;
	EB_	*EB = &SYSTEM.EB;
	
	for(i = 0, sat_tracked = 0; i < 8; i++)
	{
		if(SYSTEM.TRACK.SAT[i].MODE == 8 &&		// avail for position
			SYSTEM.TRACK.SAT[i].SS > 0 &&		// signal strength
			SYSTEM.TRACK.SAT[i].STATUS&0x80 )	// using for position fix
		{
			sat_tracked++;
		}
	}
	
	if(SYSTEM.TRACK.TRACKED > 0) sat_tracked = SYSTEM.TRACK.TRACKED;
	
	if(sat_tracked)
	{
		EB->zero_tracking = 0;
	}
	else 
	{
		if(SYSTEM.EB.ANTENNA == ANTENNA_OK)	EB->zero_tracking++;
		else EB->zero_tracking = 0;
		
		//40mins top
		if(EB->zero_tracking > sync15_reset_min*60)
		{
			EB->zero_tracking = 0;
			
			if(reset_type_toggle == 0) {
				skipping_change_eb = 1;
				//LogIt3(LOG_SYNC15_RESET, RESET_MODEL_SYNC15, RESET_TYPE_INITSTEP, 8);
				LogIt3(LOG_CATA1_NORM, LOG_CATA2_WARN, LOG_SYNC15_RESET, RESET_MODEL_SYNC15, RESET_TYPE_INITSTEP, 8);
				Init_EB(INIT_EB_SILENT, INIT_EB_SECOND, NO);

				EB_Task_start();
			}
			else{
				skipping_change_eb = 1;
				//LogIt3(LOG_SYNC15_RESET, RESET_MODEL_SYNC15, RESET_TYPE_ALMCLEAR, 9);
				LogIt3(LOG_CATA1_NORM, LOG_CATA2_WARN, LOG_SYNC15_RESET, RESET_MODEL_SYNC15, RESET_TYPE_ALMCLEAR, 9);
				RequestEB(eDEFAULT);
			
				Init_EB(INIT_EB_SILENT, INIT_EB_SECOND, NO);
				EB_Task_start();
			}
			
			if(reset_type_toggle == 0) {reset_type_toggle = 1;}
			else {reset_type_toggle = 0;}
				
			if(SYSTEM.OPERATION.OPMODE == OPMODE_OPERATION){
				RECOVERY.flag = ON;
				RECOVERY.IntervalCounter = 0;
				RECOVERY.wait_time = 20;
				RECOVERY.average_time = 20;
			}
		}

	}
}


int eb_unlock_cnt = 0;
int eb_lock_cnt = 0;
int eb_toggle = 0;
int eb_10min_start = 0;

void eb_10min_off(void)
{
	char i;
	char sat_tracked;
	
	if(!eb_10min_start) {return;}
	
	if(SYSTEM.EB.STATUS == EB_STATUS_RUN){
		
		eb_unlock_cnt++;
		
		if(CHECKLOCK.LockedCounter){
		//if(EB->LOCKMODE != EB_UNLOCKED){
			eb_lock_cnt++;	
			if(eb_lock_cnt>5*60){eb_unlock_cnt = 0;}
			if(eb_lock_cnt>30*60){eb_toggle = 0;}
		}
		else{
			eb_lock_cnt = 0;
		}
		
		if(eb_unlock_cnt>10*60 && eb_lock_cnt == 0){
			eb_lock_cnt = 0;
			eb_unlock_cnt = 0;
			 
			if(eb_toggle == 0) {
				skipping_change_eb = 1;
				//LogIt3(LOG_SYNC15_RESET, RESET_MODEL_SYNC15, RESET_TYPE_INITSTEP, 10);
				LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_SYNC15_RESET, RESET_MODEL_SYNC15, RESET_TYPE_INITSTEP, 10);
				
				eb_toggle = 1;

				
				Init_EB(INIT_EB_SILENT, INIT_EB_SECOND, NO);
				EB_Task_start();
			}
			else {
				skipping_change_eb = 1;
				//LogIt3(LOG_SYNC15_RESET, RESET_MODEL_SYNC15, RESET_TYPE_ALMCLEAR, 11);
				LogIt3(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_SYNC15_RESET, RESET_MODEL_SYNC15, RESET_TYPE_ALMCLEAR, 11);
				
				eb_toggle = 0;
				RequestEB(eDEFAULT);
				
				Init_EB(INIT_EB_SILENT, INIT_EB_SECOND, NO);
				EB_Task_start();
			}
			
			if(SYSTEM.OPERATION.OPMODE == OPMODE_OPERATION){
				RECOVERY.flag = ON;
				RECOVERY.IntervalCounter = 0;
				RECOVERY.wait_time = 20;
				RECOVERY.average_time = 20;
			}
		}
	}
	else{
		eb_unlock_cnt = 0;
		eb_lock_cnt = 0;
	}
}



